package com.demo.controllers;

import com.demo.database.data.TDemoDepartment;
import com.demo.database.data.TDemoPosition;
import com.demo.services.IDepartmentService;
import com.demo.services.IPositionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门管理控制器
 * @cuthor luoxiaojiang
 * @createTime 2019年11月17日20:45:32
 * @Verson 1.0.0
 */
@Controller
public class DepartmentController {

        //注入职位管理业务接口对象
        @Resource
        private IDepartmentService idepartmentService;

        /**
         * 查询所有职位数据
         * @param model
         * @return
         */
        @RequestMapping("/departmenQurey.html")
        public String positionQurey(Model model, @RequestParam("pageNum") Integer pageNum){
            try {
                if (pageNum == null || pageNum < 1) {
                    pageNum = 1;// 页码，从1开始
                }
                Integer pageSize = 8;// 每页显示数据
                PageHelper.startPage(pageNum, pageSize);// 指定开始分页
                List<TDemoDepartment> departmentList = idepartmentService.departmentQuery();
                PageInfo<TDemoDepartment> page = new PageInfo<>(departmentList);
                //封装模型数据
                model.addAttribute("page",page);
                //返回下个视图页面
                return "admin/department/departmentIndex";
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }

        /**
         * 按模糊条件查询职位数据
         * @param searchContext
         * @param model
         * @return
         */
        @RequestMapping("/departmenSearch.html")
        public String employeeSearch(String searchContext, Model model, @RequestParam("pageNum") Integer pageNum){
            try {
                if (pageNum == null || pageNum < 1) {
                    pageNum = 1;// 页码，从1开始
                }
                Integer pageSize = 8;// 每页显示数据
                PageHelper.startPage(pageNum, pageSize);// 指定开始分页
                //访问业务实现接口，查询数据
                List<TDemoDepartment> departmentList = idepartmentService.departmentQuery1(searchContext);
                PageInfo<TDemoDepartment> page = new PageInfo<>(departmentList);
                //封装模型数据
                model.addAttribute("page",page);
                model.addAttribute("operating","search");
                model.addAttribute("searchContext",searchContext);
                //返回下个视图页面
                return "admin/department/departmentIndex";
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }

        /**
         * 打开添加职位视图页面
         * @param model
         * @return
         */
        @RequestMapping("/addDepartmenView.html")
        public String addView(Model model,@RequestParam("num") Integer num){
            try{
                model.addAttribute("num",num);
                return "admin/department/departmentAdd";
            } catch (Exception e){
                e.printStackTrace();
            }
            return "error";
        }

        /**
         * 添加职位数据
         * @param department
         * @param model
         */
        @RequestMapping("/departmenAdd.html")
        public String employeeAdd(TDemoDepartment department, Model model, @RequestParam("pageNum") Integer pageNum){
            try {
                if (pageNum == null || pageNum < 1) {
                    pageNum = 1;// 页码，从1开始
                }
                Integer pageSize = 8;// 每页显示数据
                PageHelper.startPage(pageNum, pageSize);// 指定开始分页
                List<TDemoDepartment> departmentList = idepartmentService.departmentAdd(department);
                PageInfo<TDemoDepartment> page = new PageInfo<>(departmentList);
                //封装模型数据
                model.addAttribute("page",page);
                return "admin/department/departmentIndex";
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }

        /**
         * 删除职位数据
         * @param model
         * @param departmentId
         * @return
         */
        @RequestMapping("/departmenDel.html")
        public String employeeDel(Model model,@RequestParam("pageNum") Integer pageNum,@RequestParam("departmentId") String... departmentId){
            try {
                if (pageNum == null || pageNum < 1) {
                    pageNum = 1;// 页码，从1开始
                }
                Integer pageSize = 8;// 每页显示数据
                PageHelper.startPage(pageNum, pageSize);// 指定开始分页
                List<TDemoDepartment> departmentList = idepartmentService.departmentDel(departmentId);
                PageInfo<TDemoDepartment> page = new PageInfo<>(departmentList);
                //封装模型数据
                model.addAttribute("page",page);
                return "admin/department/departmentIndex";
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }

        /**
         * 打开修改页面的视图
         * @param departmentId
         * @param model
         * @return
         */
        @RequestMapping("/updDepartmenView.html")
        public String udpView(Integer departmentId, Model model,@RequestParam("num") Integer num){
            try {
                TDemoDepartment department = idepartmentService.departmentQueryID(departmentId);
                model.addAttribute("department",department);
                model.addAttribute("num",num);
                return "admin/department/departmentUdp";
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }

        @RequestMapping("/departmenUpd.html")
        public String employeeUpd(TDemoDepartment department, Model model, @RequestParam("pageNum") Integer pageNum){
            try {
                if (pageNum == null || pageNum < 1) {
                    pageNum = 1;// 页码，从1开始
                }
                Integer pageSize = 8;// 每页显示数据
                PageHelper.startPage(pageNum, pageSize);// 指定开始分页
                List<TDemoDepartment> departmentList = idepartmentService.departmentUpd(department);
                PageInfo<TDemoDepartment> page = new PageInfo<>(departmentList);
                //封装模型数据
                model.addAttribute("page",page);
                return "admin/department/departmentIndex";
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }
}
